﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DTcms.DBUtility;
using DTcms.Common;

namespace DTcms.DAL
{
    /// <summary>
    /// 数据访问类:ch_stone_orders
    /// </summary>
    public partial class stone_ordersline
    {
		public stone_ordersline()
		{ }
        #region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(Guid id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ch_stone_ordersline");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.UniqueIdentifier,16)          };
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.stone_ordersline model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ch_stone_ordersline(");
            strSql.Append("id,itemNo,order_id,orderNo,goods_no,goods_title,img_url,goods_price,real_price,quantity,unit,long,wide,high,model,thickness,square,express_fee,add_time,extend1,extend2,extend3,extend4,extend5,extend6)");
            strSql.Append(" values (");
            strSql.Append("@id,@itemNo,@order_id,@orderNo,@goods_no,@goods_title,@img_url,@goods_price,@real_price,@quantity,@unit,@long,@wide,@high,@model,@thickness,@square,@express_fee,@add_time,@extend1,@extend2,@extend3,@extend4,@extend5,@extend6)");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.UniqueIdentifier,16),
                    new SqlParameter("@itemNo", SqlDbType.Int,4),
                    new SqlParameter("@order_id", SqlDbType.UniqueIdentifier,16),
                    new SqlParameter("@orderNo", SqlDbType.NVarChar,50),
                    new SqlParameter("@goods_no", SqlDbType.NVarChar,50),
                    new SqlParameter("@goods_title", SqlDbType.NVarChar,200),
                    new SqlParameter("@img_url", SqlDbType.NVarChar,255),
                    new SqlParameter("@goods_price", SqlDbType.Decimal,5),
                    new SqlParameter("@real_price", SqlDbType.Decimal,5),
                    new SqlParameter("@quantity", SqlDbType.Int,4),
                    new SqlParameter("@unit", SqlDbType.NVarChar,50),
                    new SqlParameter("@long", SqlDbType.NVarChar,50),
                    new SqlParameter("@wide", SqlDbType.NVarChar,50),
                    new SqlParameter("@high", SqlDbType.NVarChar,50),
                    new SqlParameter("@model", SqlDbType.NVarChar,50),
                    new SqlParameter("@thickness", SqlDbType.Decimal,5),
                    new SqlParameter("@square", SqlDbType.Decimal,5),
                    new SqlParameter("@express_fee", SqlDbType.Decimal,5),
                    new SqlParameter("@add_time", SqlDbType.DateTime),
                    new SqlParameter("@extend1", SqlDbType.Decimal,5),
                    new SqlParameter("@extend2", SqlDbType.Decimal,5),
                    new SqlParameter("@extend3", SqlDbType.NVarChar,50),
                    new SqlParameter("@extend4", SqlDbType.NVarChar,50),
                    new SqlParameter("@extend5", SqlDbType.NVarChar,50),
                    new SqlParameter("@extend6", SqlDbType.NVarChar,50)};
            parameters[0].Value = Guid.NewGuid();
            parameters[1].Value = model.itemNo;
            parameters[2].Value = model.order_id;
            parameters[3].Value = model.orderNo;
            parameters[4].Value = model.goods_no;
            parameters[5].Value = model.goods_title;
            parameters[6].Value = model.img_url;
            parameters[7].Value = model.goods_price;
            parameters[8].Value = model.real_price;
            parameters[9].Value = model.quantity;
            parameters[10].Value = model.unit;
            parameters[11].Value = model.Long;
            parameters[12].Value = model.wide;
            parameters[13].Value = model.high;
            parameters[14].Value = model.model;
            parameters[15].Value = model.thickness;
            parameters[16].Value = model.square;
            parameters[17].Value = model.express_fee;
            parameters[18].Value = model.add_time;
            parameters[19].Value = model.extend1;
            parameters[20].Value = model.extend2;
            parameters[21].Value = model.extend3;
            parameters[22].Value = model.extend4;
            parameters[23].Value = model.extend5;
            parameters[24].Value = model.extend6;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.stone_ordersline model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ch_stone_ordersline set ");
            strSql.Append("itemNo=@itemNo,");
            strSql.Append("order_id=@order_id,");
            strSql.Append("orderNo=@orderNo,");
            strSql.Append("goods_no=@goods_no,");
            strSql.Append("goods_title=@goods_title,");
            strSql.Append("img_url=@img_url,");
            strSql.Append("goods_price=@goods_price,");
            strSql.Append("real_price=@real_price,");
            strSql.Append("quantity=@quantity,");
            strSql.Append("unit=@unit,");
            strSql.Append("long=@long,");
            strSql.Append("wide=@wide,");
            strSql.Append("high=@high,");
            strSql.Append("model=@model,");
            strSql.Append("thickness=@thickness,");
            strSql.Append("square=@square,");
            strSql.Append("express_fee=@express_fee,");
            strSql.Append("add_time=@add_time,");
            strSql.Append("extend1=@extend1,");
            strSql.Append("extend2=@extend2,");
            strSql.Append("extend3=@extend3,");
            strSql.Append("extend4=@extend4,");
            strSql.Append("extend5=@extend5,");
            strSql.Append("extend6=@extend6");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@itemNo", SqlDbType.Int,4),
                    new SqlParameter("@order_id", SqlDbType.UniqueIdentifier,16),
                    new SqlParameter("@orderNo", SqlDbType.NVarChar,50),
                    new SqlParameter("@goods_no", SqlDbType.NVarChar,50),
                    new SqlParameter("@goods_title", SqlDbType.NVarChar,200),
                    new SqlParameter("@img_url", SqlDbType.NVarChar,255),
                    new SqlParameter("@goods_price", SqlDbType.Decimal,5),
                    new SqlParameter("@real_price", SqlDbType.Decimal,5),
                    new SqlParameter("@quantity", SqlDbType.Int,4),
                    new SqlParameter("@unit", SqlDbType.NVarChar,50),
                    new SqlParameter("@long", SqlDbType.NVarChar,50),
                    new SqlParameter("@wide", SqlDbType.NVarChar,50),
                    new SqlParameter("@high", SqlDbType.NVarChar,50),
                    new SqlParameter("@model", SqlDbType.NVarChar,50),
                    new SqlParameter("@thickness", SqlDbType.Decimal,5),
                    new SqlParameter("@square", SqlDbType.Decimal,5),
                    new SqlParameter("@express_fee", SqlDbType.Decimal,5),
                    new SqlParameter("@add_time", SqlDbType.DateTime),
                    new SqlParameter("@extend1", SqlDbType.Decimal,5),
                    new SqlParameter("@extend2", SqlDbType.Decimal,5),
                    new SqlParameter("@extend3", SqlDbType.NVarChar,50),
                    new SqlParameter("@extend4", SqlDbType.NVarChar,50),
                    new SqlParameter("@extend5", SqlDbType.NVarChar,50),
                    new SqlParameter("@extend6", SqlDbType.NVarChar,50),
                    new SqlParameter("@id", SqlDbType.UniqueIdentifier,16)};
            parameters[0].Value = model.itemNo;
            parameters[1].Value = model.order_id;
            parameters[2].Value = model.orderNo;
            parameters[3].Value = model.goods_no;
            parameters[4].Value = model.goods_title;
            parameters[5].Value = model.img_url;
            parameters[6].Value = model.goods_price;
            parameters[7].Value = model.real_price;
            parameters[8].Value = model.quantity;
            parameters[9].Value = model.unit;
            parameters[10].Value = model.Long;
            parameters[11].Value = model.wide;
            parameters[12].Value = model.high;
            parameters[13].Value = model.model;
            parameters[14].Value = model.thickness;
            parameters[15].Value = model.square;
            parameters[16].Value = model.express_fee;
            parameters[17].Value = model.add_time;
            parameters[18].Value = model.extend1;
            parameters[19].Value = model.extend2;
            parameters[20].Value = model.extend3;
            parameters[21].Value = model.extend4;
            parameters[22].Value = model.extend5;
            parameters[23].Value = model.extend6;
            parameters[24].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(Guid id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ch_stone_ordersline ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.UniqueIdentifier,16)          };
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ch_stone_ordersline ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.stone_ordersline GetModel(Guid id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,itemNo,order_id,orderNo,goods_no,goods_title,img_url,goods_price,real_price,quantity,unit,long,wide,high,model,thickness,square,express_fee,add_time,extend1,extend2,extend3,extend4,extend5,extend6 from ch_stone_ordersline ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.UniqueIdentifier,16)          };
            parameters[0].Value = id;

            Model.stone_ordersline model = new Model.stone_ordersline();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.stone_ordersline DataRowToModel(DataRow row)
        {
            Model.stone_ordersline model = new Model.stone_ordersline();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = new Guid(row["id"].ToString());
                }
                if (row["itemNo"] != null && row["itemNo"].ToString() != "")
                {
                    model.itemNo = int.Parse(row["itemNo"].ToString());
                }
                if (row["order_id"] != null && row["order_id"].ToString() != "")
                {
                    model.order_id = new Guid(row["order_id"].ToString());
                }
                if (row["orderNo"] != null)
                {
                    model.orderNo = row["orderNo"].ToString();
                }
                if (row["goods_no"] != null)
                {
                    model.goods_no = row["goods_no"].ToString();
                }
                if (row["goods_title"] != null)
                {
                    model.goods_title = row["goods_title"].ToString();
                }
                if (row["img_url"] != null)
                {
                    model.img_url = row["img_url"].ToString();
                }
                if (row["goods_price"] != null && row["goods_price"].ToString() != "")
                {
                    model.goods_price = decimal.Parse(row["goods_price"].ToString());
                }
                if (row["real_price"] != null && row["real_price"].ToString() != "")
                {
                    model.real_price = decimal.Parse(row["real_price"].ToString());
                }
                if (row["quantity"] != null && row["quantity"].ToString() != "")
                {
                    model.quantity = int.Parse(row["quantity"].ToString());
                }
                if (row["unit"] != null)
                {
                    model.unit = row["unit"].ToString();
                }
                if (row["long"] != null)
                {
                    model.Long= row["long"].ToString();
                }
                if (row["wide"] != null)
                {
                    model.wide = row["wide"].ToString();
                }
                if (row["high"] != null)
                {
                    model.high = row["high"].ToString();
                }
                if (row["model"] != null)
                {
                    model.model = row["model"].ToString();
                }
                if (row["thickness"] != null && row["thickness"].ToString() != "")
                {
                    model.thickness = decimal.Parse(row["thickness"].ToString());
                }
                if (row["square"] != null && row["square"].ToString() != "")
                {
                    model.square = decimal.Parse(row["square"].ToString());
                }
                if (row["express_fee"] != null && row["express_fee"].ToString() != "")
                {
                    model.express_fee = decimal.Parse(row["express_fee"].ToString());
                }
                if (row["add_time"] != null && row["add_time"].ToString() != "")
                {
                    model.add_time = DateTime.Parse(row["add_time"].ToString());
                }
                if (row["extend1"] != null && row["extend1"].ToString() != "")
                {
                    model.extend1 = decimal.Parse(row["extend1"].ToString());
                }
                if (row["extend2"] != null && row["extend2"].ToString() != "")
                {
                    model.extend2 = decimal.Parse(row["extend2"].ToString());
                }
                if (row["extend3"] != null)
                {
                    model.extend3 = row["extend3"].ToString();
                }
                if (row["extend4"] != null)
                {
                    model.extend4 = row["extend4"].ToString();
                }
                if (row["extend5"] != null)
                {
                    model.extend5 = row["extend5"].ToString();
                }
                if (row["extend6"] != null)
                {
                    model.extend6 = row["extend6"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,itemNo,order_id,orderNo,goods_no,goods_title,img_url,goods_price,real_price,quantity,unit,long,wide,high,model,thickness,square,express_fee,add_time,extend1,extend2,extend3,extend4,extend5,extend6 ");
            strSql.Append(" FROM ch_stone_ordersline ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,itemNo,order_id,orderNo,goods_no,goods_title,img_url,goods_price,real_price,quantity,unit,long,wide,high,model,thickness,square,express_fee,add_time,extend1,extend2,extend3,extend4,extend5,extend6 ");
            strSql.Append(" FROM ch_stone_ordersline ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM ch_stone_ordersline ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from ch_stone_ordersline T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "ch_stone_ordersline";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod
       
        #endregion  ExtensionMethod


    }
}
